package com.company.ecis.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.company.ecis.entity.BalanceInfo;
import com.company.ecis.entity.Userinfo;
import com.company.ecis.entity.vo.UserinfoSmallVO;
import com.company.ecis.entity.vo.UserinfoVO;
import com.company.ecis.service.BalanceInfoService;
import com.company.ecis.service.UserinfoService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.UUID;

/**
 * @author : 杨宇
 * @date : 2022-08-04 11:37
 **/
@RestController
public class AuthController {
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private BalanceInfoService balanceInfoService;

    @PostMapping("/login")
    public String login(UserinfoVO userinfoVO, @RequestParam("captcha") String captcha, HttpServletRequest request) {
        JSONObject jsonObject = JSONUtil.createObj();
        HttpSession session = request.getSession();
        UserinfoSmallVO smallVO = new UserinfoSmallVO();
        // 判断验证码是否一致
        if (!Objects.equals(captcha,session.getAttribute(ServletUtil.getClientIP(request))))
        {
            jsonObject.set("code",402).set("msg","验证码不一致");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 调用 Service 验证用户名和密码是否相同
        Userinfo userInfo =  userinfoService.selectOneByUsernameAndPassword(userinfoVO);
        // 判断 返回值是否为空 空值代表用户不存在或账号密码不正确
        if (userInfo == null){
            jsonObject.set("code",401)
                    .set("msg","登录失败");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 将用户全部信息存储到Session，以用户名获取
        session.setAttribute(userInfo.getUsername(),userInfo);
        session.setAttribute(userInfo.getId().toString(),userInfo);
        session.setAttribute("余额用户信息",userInfo);
        // 将用户实体转换到 vo 返回给前端
        BeanUtil.copyProperties(userInfo,smallVO);
        jsonObject.set("code",200).set("msg","登录成功").set("data",smallVO);
        return JSONUtil.toJsonStr(jsonObject);
    }

    @PostMapping("/reg")
    public String register(Userinfo userinfo) {
        JSONObject jsonObject = JSONUtil.createObj();
        userinfo.setCardId(IdUtil.simpleUUID());
        if (!userinfoService.insertUserinfo(userinfo)) {
            jsonObject.set("code",401).set("msg","用户已存在");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 加上事务管理
        // 为账户添加余额 初始为零
        BalanceInfo balanceInfo = new BalanceInfo();
        balanceInfo.setBalance(BigDecimal.ZERO);
        balanceInfo.setUsername(userinfo.getUsername());
        balanceInfoService.insertBalance(balanceInfo);
        jsonObject.set("code",200).set("msg","注册成功");
        return JSONUtil.toJsonStr(jsonObject);
    }
}
